﻿Imports TepcelDominio
Imports TepcelBL
Public Class frmAlmacen
    Dim err As New System.Windows.Forms.ErrorProvider
    Dim listaAlmacen As New List(Of Almacen)
    Dim UbigeoBL As New UbigeoBL
    Dim AlmacenBL As New AlmacenBL
    Private Sub frmAlmacen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call CargarGrid()
        txtBuscar.Focus()
    End Sub

    Private Sub clearError()
        err.SetError(txtNombreAlmacen, "")
        err.SetError(CBEDepartamento, "")
        err.SetError(CBEDistrito, "")
        err.SetError(CBEProvincia, "")
    End Sub

    Private Sub CargarGrid()
        DGVLista.Rows.Clear()
        listaAlmacen = AlmacenBL.GetAllFromAlmacen
        If listaAlmacen.Count = 0 Then
            btnModificar.Enabled = False
            btnEliminar.Enabled = False
        End If
        Dim i As Integer
        For i = 0 To listaAlmacen.Count - 1
            With listaAlmacen.Item(i)
                Dim ubigeo As Ubigeo = UbigeoBL.GetByIdFromUbigeo(.codigoUbigeo)
                DGVLista.Rows.Add(i, ubigeo.codigoUbigeo, ubigeo.dependenciaUbigeo.codigoUbigeo, ubigeo.dependenciaUbigeo.dependenciaUbigeo.codigoUbigeo,
                                  .codigoAlmacen, .nombreAlmacen, ubigeo.nombreUbigeo, ubigeo.dependenciaUbigeo.nombreUbigeo, ubigeo.dependenciaUbigeo.dependenciaUbigeo.nombreUbigeo)

            End With
        Next
    End Sub

    Private Sub nuevo()
        btnModificar.Enabled = False
        btnEliminar.Enabled = False
        btnCancelar.Enabled = True
        btnGuardar.Enabled = True
        btnNuevo.Enabled = False

        txtNumeroSerie.Enabled = True
        CBEDepartamento.Enabled = True
        CBEProvincia.Enabled = True
        CBEDistrito.Enabled = True
        txtNombreAlmacen.Enabled = True
        txtTelefonoAlmacen.Enabled = True

        DGVLista.Enabled = False

        Call CargarListaDepartamentos()

        txtNumeroSerie.Focus()
    End Sub

    Private Sub CargarListaDepartamentos()
        Dim listaDepartamento As New List(Of Ubigeo)

        listaDepartamento = UbigeoBL.GetAllFromUbigeo(1)

        CBEDepartamento.DataSource = Nothing
        CBEDepartamento.DataSource = listaDepartamento
        CBEDepartamento.DisplayMember = "nombreUbigeo"
        CBEDepartamento.ValueMember = "codigoUbigeo"

    End Sub

    Private Sub cancelar()
        btnModificar.Enabled = True
        btnEliminar.Enabled = True
        btnCancelar.Enabled = False
        btnGuardar.Enabled = False
        btnNuevo.Enabled = True

        txtNumeroSerie.Enabled = False
        CBEDepartamento.Enabled = False
        CBEProvincia.Enabled = False
        CBEDistrito.Enabled = False
        txtNombreAlmacen.Enabled = False
        txtTelefonoAlmacen.Enabled = False

        DGVLista.Enabled = True

        txtCodigoAlmacen.Text = ""
        txtNumeroSerie.Text = ""
        CBEDepartamento.Text = ""
        CBEProvincia.Text = ""
        CBEDistrito.Text = ""
        txtNombreAlmacen.Text = ""
        txtTelefonoAlmacen.Text = ""
    End Sub

    Private Sub txtBuscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.TextChanged
        If txtBuscar.Text.Trim.Length = 0 Then
            Call CargarGrid()
        Else
            DGVLista.Rows.Clear()
            listaAlmacen = AlmacenBL.shearchFromAlmacen(txtBuscar.Text.Trim)
            Dim i As Integer
            For i = 0 To listaAlmacen.Count - 1
                With listaAlmacen.Item(i)
                    Dim ubigeo As Ubigeo = UbigeoBL.GetByIdFromUbigeo(.codigoUbigeo)
                    DGVLista.Rows.Add(i, ubigeo.codigoUbigeo, ubigeo.dependenciaUbigeo.codigoUbigeo, ubigeo.dependenciaUbigeo.dependenciaUbigeo.codigoUbigeo,
                                      .codigoAlmacen, .nombreAlmacen, ubigeo.nombreUbigeo, ubigeo.dependenciaUbigeo.nombreUbigeo, ubigeo.dependenciaUbigeo.dependenciaUbigeo.nombreUbigeo)

                End With
            Next
        End If
    End Sub

    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click
        Call nuevo()
        Call clearError()
        Dim item As Integer = DGVLista.Item(0, DGVLista.CurrentRow.Index).Value
        CBEDepartamento.SelectedValue = DGVLista.Item(3, DGVLista.CurrentRow.Index).Value
        CBEProvincia.SelectedValue = DGVLista.Item(2, DGVLista.CurrentRow.Index).Value
        With listaAlmacen.Item(item)
            txtCodigoAlmacen.Text = .codigoAlmacen
            txtNumeroSerie.Text = .serieAlmacen
            CBEDistrito.SelectedValue = .codigoUbigeo
            txtNombreAlmacen.Text = .nombreAlmacen
            txtTelefonoAlmacen.Text = .telefonoAlmacen
        End With

    End Sub

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If CBEDepartamento.Text.Trim.Length = 0 Then
            err.SetError(CBEDepartamento, "Seleccione un Departamento")
            Return
        Else
            err.SetError(CBEDepartamento, "")
        End If

        If CBEProvincia.Text.Trim.Length = 0 Then
            err.SetError(CBEProvincia, "Seleccione una Provincia")
            Return
        Else
            err.SetError(CBEProvincia, "")
        End If

        If CBEDistrito.Text.Trim.Length = 0 Then
            err.SetError(CBEDistrito, "Seleccione un Distrito")
            Return
        Else
            err.SetError(CBEDistrito, "")
        End If

        If txtNombreAlmacen.Text.Trim.Length = 0 Then
            err.SetError(txtNombreAlmacen, "LLene el nombre del Almacen")
            Return
        Else
            err.SetError(txtNombreAlmacen, "")
        End If


        If (txtCodigoAlmacen.Text.Trim.Length > 0) Then
            Dim alm As New TepcelDominio.Almacen(txtCodigoAlmacen.Text, txtNombreAlmacen.Text.Trim, txtTelefonoAlmacen.Text.Trim, CBEDistrito.SelectedValue.Trim, txtNumeroSerie.Text.Trim)
            txtCodigoAlmacen.Text = AlmacenBL.NuevoAlmacen(alm)
        Else
            Dim alm As New TepcelDominio.Almacen("", txtNombreAlmacen.Text.Trim, txtTelefonoAlmacen.Text.Trim, CBEDistrito.SelectedValue.Trim, txtNumeroSerie.Text.Trim)
            txtCodigoAlmacen.Text = AlmacenBL.NuevoAlmacen(alm)
        End If


        Call cancelar()


        Call CargarGrid()

    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        Dim result As DialogResult
        result = MessageBox.Show("confirma que desea Eliminar el Registro seleccionado?", "Eliminar", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
        If result = DialogResult.OK Then
            If AlmacenBL.DeleteAlmacen(listaAlmacen.Item(CInt(DGVLista.Item(0, DGVLista.CurrentRow.Index).Value)).codigoAlmacen) Then
                MessageBox.Show("Registro Eliminado Correctamente", "Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Call CargarGrid()
            Else
                MessageBox.Show("No se elimino el registro seleccionado", "Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Call cancelar()
        Call clearError()

        txtBuscar.Focus()

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        Call clearError()
        Call nuevo()
    End Sub

    Private Sub CBEDepartamento_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBEDepartamento.GotFocus, CBEProvincia.GotFocus, CBEDistrito.GotFocus
        sender.SelectionStart = 0
        sender.SelectionLength = sender.Text.Length
    End Sub

    Private Sub CBEDepartamento_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CBEDepartamento.KeyUp, CBEProvincia.KeyUp, CBEDistrito.KeyUp
        Dim sTexto As String
        Dim indiceEncontrado As Integer
        Dim itemEncontrado As Object
        Dim textoItem As String
        Dim agregarTexto As String


        Select Case e.KeyCode
            Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down, Keys.Tab, Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp, Keys.Shift
                Return
        End Select

        sTexto = sender.Text
        indiceEncontrado = sender.FindString(sTexto)

        If indiceEncontrado >= 0 Then
            itemEncontrado = sender.Items(indiceEncontrado)
            textoItem = sender.GetItemText(itemEncontrado)

            agregarTexto = textoItem.Substring(sTexto.Length)
            sender.Text = sTexto & agregarTexto

            sender.SelectionStart = sTexto.Length
            sender.SelectionLength = agregarTexto.Length

        End If

    End Sub

    Private Sub CargarListaProvincias()
        Try
            Dim val As String = CBEDepartamento.SelectedValue
        Catch ex As Exception
            Return
        End Try

        Dim listaProvincia As New List(Of Ubigeo)
        listaProvincia = UbigeoBL.GetAllFromUbigeo(2)
        Dim i As Integer = 0
        While i < listaProvincia.Count
            If listaProvincia.Item(0).dependenciaUbigeo.codigoUbigeo <> CBEDepartamento.SelectedValue Then
                listaProvincia.RemoveAt(i)
                i = i - 1
            End If
            i = i + 1
        End While
        CBEProvincia.DataSource = Nothing
        CBEProvincia.DataSource = listaProvincia
        CBEProvincia.DisplayMember = "nombreUbigeo"
        CBEProvincia.ValueMember = "codigoUbigeo"

    End Sub

    Private Sub CargarListaDistritos()
        Try
            Dim val As String = CBEProvincia.SelectedValue
        Catch ex As Exception
            Return
        End Try

        Dim listaDistrito As New List(Of Ubigeo)
        listaDistrito = UbigeoBL.GetAllFromUbigeo(3)
        Dim i As Integer = 0
        While i < listaDistrito.Count
            If listaDistrito.Item(i).dependenciaUbigeo.codigoUbigeo <> CBEProvincia.SelectedValue Then
                listaDistrito.RemoveAt(i)
                i = i - 1
            End If
            i = i + 1
        End While
        CBEDistrito.DataSource = Nothing
        CBEDistrito.DataSource = listaDistrito
        CBEDistrito.DisplayMember = "nombreUbigeo"
        CBEDistrito.ValueMember = "codigoUbigeo"

    End Sub

    Private Sub CBEDepartamento_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBEDepartamento.SelectedValueChanged
        Call CargarListaProvincias()
    End Sub

    Private Sub CBEProvincia_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBEProvincia.SelectedValueChanged
        Call CargarListaDistritos()
    End Sub

End Class